/***
 * Excerpted from "Stripes: and Java Web Development is Fun Again",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
***/
package stripesbook.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Message;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.action.StreamingResolution;
import org.stripesbook.rayures.action.RayuresBaseActionBean;
import org.stripesbook.rayures.util.Constants;
import org.stripesbook.rayures.view.stripes.SLink;
import org.stripesbook.rayures.view.stripes.SParam;
import stripesbook.dao.AttachmentDao;
import stripesbook.dao.ContactDao;
import stripesbook.dao.FolderDao;
import stripesbook.dao.MessageDao;
import stripesbook.dao.RoleDao;
import stripesbook.dao.UserDao;
import stripesbook.dao.impl.stripersist.AttachmentDaoImpl;
import stripesbook.dao.impl.stripersist.ContactDaoImpl;
import stripesbook.dao.impl.stripersist.FolderDaoImpl;
import stripesbook.dao.impl.stripersist.MessageDaoImpl;
import stripesbook.dao.impl.stripersist.RoleDaoImpl;
import stripesbook.dao.impl.stripersist.UserDaoImpl;
import stripesbook.ext.MyActionBeanContext;
import stripesbook.model.Folder;
import stripesbook.model.User;
import stripesbook.view.Menu;

public abstract class BaseActionBean extends RayuresBaseActionBean {
    private MyActionBeanContext context;

    public MyActionBeanContext getContext() {
        return context;
    }
    public void setContext(ActionBeanContext context) {
        this.context = (MyActionBeanContext) context;
    }
    public Menu.Section getCurrentSection() {
      return null;
    }
    public void setFolder(Folder folder) {
        if (getUser().equals(folder.getUser())) {
            getContext().setCurrentFolder(folder);
        }
    }
    public Resolution stream(Object object) {
      return new StreamingResolution("text/html", object.toString());
    }
    public SLink getLanguageLink() {
      String value = bundle.get("layout.otherLocale");
      String label = bundle.get("layout.otherLanguage");
      return tag(SLink.class).href(getLastUrl()).label(label).add(
        tag(SParam.class).name(Constants.DEFAULT_LANGUAGE_PARAM).value(value));
    }
    @SuppressWarnings("unchecked")
    public String getLastUrl() {
        HttpServletRequest req = getContext().getRequest();
        StringBuilder sb = new StringBuilder();

        // Start with the URI and the path
        String uri = (String)
            req.getAttribute("javax.servlet.forward.request_uri");
        String path = (String)
            req.getAttribute("javax.servlet.forward.path_info");
        if (uri == null) {
            uri = req.getRequestURI(); 
            path = req.getPathInfo(); 
        }
        sb.append(uri);
        if (path != null) { sb.append(path); }

        // Now the request parameters
        sb.append('?');
        Map<String,String[]> map =
            new HashMap<String,String[]>(req.getParameterMap());

        // Remove previous locale parameter, if present.
        map.remove("lang");

        // Append the parameters to the URL
        for (String key : map.keySet()) {
            String[] values = map.get(key);
            for (String value : values) {
                sb.append(key).append('=').append(value).append('&');
            }
        }
        // Remove the last '&'
        sb.deleteCharAt(sb.length() - 1);

        return sb.toString();
    }
    protected Message getLocalizableMessage(String key,
        Object... parameters)
    {
        return new SimpleMessage(bundle.get(key, parameters));
    }
    public User getUser() {
        return getContext().getUser();
    }

    protected AttachmentDao attachmentDao = new AttachmentDaoImpl();
    protected ContactDao contactDao = new ContactDaoImpl();
    protected FolderDao folderDao = new FolderDaoImpl();
    protected MessageDao messageDao = new MessageDaoImpl();
    protected RoleDao roleDao = new RoleDaoImpl();
    protected UserDao userDao = new UserDaoImpl();
}
